package com.burst.reptile.modular.system.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.burst.reptile.common.constant.UserConstants;
import com.burst.reptile.common.utils.SecurityUtils;
import com.burst.reptile.common.utils.poi.ExcelUtil;
import com.burst.reptile.framework.aspectj.lang.annotation.Log;
import com.burst.reptile.framework.aspectj.lang.enums.BusinessType;
import com.burst.reptile.framework.web.controller.BaseController;
import com.burst.reptile.framework.web.result.LayuiResult;
import com.burst.reptile.framework.web.result.Result;
import com.burst.reptile.modular.system.entity.SysRole;
import com.burst.reptile.modular.system.service.ISysRoleService;

import lombok.RequiredArgsConstructor;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author wujin
 * @since 2020-05-22
 */
@RestController
@RequestMapping("/system/role")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysRoleController extends BaseController {

	private final ISysRoleService roleService;

	@PreAuthorize("@ss.hasPermi('system:role:list')")
	@GetMapping("/list")
	public LayuiResult<Object> list(SysRole role) {
		Page<SysRole> page = startPage();
		Page<SysRole> list = roleService.selectRoleList(page, role);
		return LayuiResult.success(list.getTotal(), list.getRecords());
	}

	/**
	 * 新增角色
	 */
	@PreAuthorize("@ss.hasPermi('system:role:add')")
	@Log(title = "角色管理", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	public Result<Object> add(@Validated @RequestBody SysRole role) {
		if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))) {
			return Result.error("新增角色'" + role.getRoleName() + "'失败，角色名称已存在");
		} else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role))) {
			return Result.error("新增角色'" + role.getRoleName() + "'失败，角色权限已存在");
		}
		role.setCreateUser(SecurityUtils.getUsername());
		return Result.success(roleService.insertRole(role));

	}

	/**
	 * 修改保存角色
	 */
	@PreAuthorize("@ss.hasPermi('system:role:edit')")
	@Log(title = "角色管理", businessType = BusinessType.UPDATE)
	@PutMapping("/edit")
	public Result<Object> edit(@Validated @RequestBody SysRole role) {
		roleService.checkRoleAllowed(role);
		if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))) {
			return Result.error("修改角色'" + role.getRoleName() + "'失败，角色名称已存在");
		} else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role))) {
			return Result.error("修改角色'" + role.getRoleName() + "'失败，角色权限已存在");
		}
		role.setUpdateUser(SecurityUtils.getUsername());
		return Result.success(roleService.updateRole(role));
	}

	/**
	 * 修改保存数据权限
	 */
	@PreAuthorize("@ss.hasPermi('system:role:edit')")
	@Log(title = "角色管理", businessType = BusinessType.UPDATE)
	@PutMapping("/dataScope")
	public Result<Object> dataScope(@RequestBody SysRole role) {
		roleService.checkRoleAllowed(role);
		return Result.success(roleService.authDataScope(role));
	}

	/**
	 * 状态修改
	 */
	@PreAuthorize("@ss.hasPermi('system:role:edit')")
	@Log(title = "角色管理", businessType = BusinessType.UPDATE)
	@PutMapping("/changeStatus")
	public Result<Object> changeStatus(@RequestBody SysRole role) {
		roleService.checkRoleAllowed(role);
		return Result.success(roleService.updateRoleStatus(role));
	}

	/**
	 * 删除角色
	 */
	@PreAuthorize("@ss.hasPermi('system:role:remove')")
	@Log(title = "角色管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/remove/{roleIds}")
	public Result<Object> remove(@PathVariable Long[] roleIds) {
		return Result.success(roleService.deleteRoleByIds(roleIds));
	}

	/**
	 * 导出角色
	 * 
	 * @param role
	 * @return
	 */
	@Log(title = "角色管理", businessType = BusinessType.EXPORT)
	@PreAuthorize("@ss.hasPermi('system:role:export')")
	@GetMapping("/export")
	public Result<Object> export(SysRole role) {
		List<SysRole> list = roleService.selectRoleListExport(role);
		ExcelUtil<SysRole> util = new ExcelUtil<SysRole>(SysRole.class);
		return util.exportExcel(list, "角色数据");
	}

}
